package com.example.recall_application.mvp.model;

import android.app.Application;

import com.example.recall_application.app.Constants;
import com.example.recall_application.mvp.entity.Config;
import com.example.recall_application.mvp.entity.ConfigRequest;
import com.example.recall_application.mvp.entity.WxUserInfo;
import com.example.recall_application.mvp.model.api.Api;
import com.google.gson.Gson;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import com.jess.arms.di.scope.ActivityScope;

import javax.inject.Inject;

import com.example.recall_application.mvp.contract.MainContract;

import io.reactivex.Observable;
import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.functions.Function;
import okhttp3.ResponseBody;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 11/18/2019 14:46
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class MainModel extends BaseModel implements MainContract.Model {
    @Inject
    Gson mGson;
    @Inject
    Application mApplication;

    @Inject
    public MainModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
    }

    @Override
    public Single<Config> getConfig(ConfigRequest configRequest) {
        return mRepositoryManager.obtainRetrofitService(Api.class)
                .getConfig(configRequest);
    }

    @Override
    public Observable<Object> pullIntercept(ConfigRequest configRequest) {
        return mRepositoryManager.obtainRetrofitService(Api.class).pullIntercept(configRequest);
    }



    @Override
    public Observable<Config> upErrorLog(ConfigRequest configRequest) {
        return mRepositoryManager.obtainRetrofitService(Api.class).upErrorLog(configRequest);
    }

    @Override
    public Observable<Config> callForwarding(ConfigRequest configRequest) {
        return mRepositoryManager.obtainRetrofitService(Api.class).callForwarding(configRequest);
    }

    @Override
    public Observable<Config> getWxToken(String code) {
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token";
        String appid = Constants.APP_ID;
        String secret = Constants.AppSecret;
        String grant_type = "authorization_code";
        return mRepositoryManager
                .obtainRetrofitService(Api.class)
                .getWxToken(url,appid,secret,code,grant_type);
    }

    @Override
    public Observable<WxUserInfo> getWxUserinfo(Config config) {
        String url = "https://api.weixin.qq.com/sns/userinfo";
        return mRepositoryManager
                .obtainRetrofitService(Api.class)
                .getWxUserInfo(url,config.getAccess_token(),config.getOpenid());
    }

    @Override
    public Observable<ResponseBody> upWxUserInfo(WxUserInfo wxUserInfo) {
        return mRepositoryManager
                .obtainRetrofitService(Api.class)
                .upWxUserInfo(wxUserInfo);
    }
}